package task4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器
 */
public class Server implements Runnable{

    @Override
    public void run() {
        // 定义ServerSocket类型的对象
        ServerSocket serverSocket = null;
        Socket socket = null;
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;

        // 保证服务器一直处于开启状态
        while (true){
            try {
                // 创建ServerSocket类型的对象并提供端口号
                serverSocket = new ServerSocket(9000);
                System.out.println("当前服务器端口号为：" + serverSocket.getLocalPort() + " 等待客户机连接。。。");
                // 侦听并接收到此套接字的连接请求
                socket = serverSocket.accept();
                System.out.println("客户机 " + socket.getRemoteSocketAddress() + " 已经连接");
                // 接受客户机的输入
                objectInputStream = new ObjectInputStream(socket.getInputStream());
                UserMessage userMessage = (UserMessage) objectInputStream.readObject();
                System.out.println("该客户机传送的用户信息如下：");
                System.out.println("用户类型：" + userMessage.getType() + " 用户信息：" + userMessage.getUser().toString() );

                // 判断是否登陆成功
                String right_username = "admin";
                String right_password = "123456";
                if (userMessage.getUser().getUsername().equals(right_username) && userMessage.getUser().getPassword().equals(right_password))
                    userMessage.setType("success");
                else
                    userMessage.setType("failed");
                // System.out.print("用户类型：" + userMessage.getType() + " 用户信息：" + userMessage.getUser().toString());
                // 给客户机输出
                objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                objectOutputStream.writeObject(userMessage);

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                // 关闭对象
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (serverSocket != null){
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (objectInputStream != null){
                    try {
                        objectInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (objectOutputStream != null){
                    try {
                        objectInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        // 启动
        Thread thread = new Thread(new Server());
        thread.start();
    }
}
